/////////////////////////////////////////////
// ObjectARX defined commands

#include "StdAfx.h"
#include "StdArx.h"


// This is command 'WINDO'
void windo()
{
	AcDbDatabase *pCurDb;
	AcDbObjectId windId1, windId2;
	AcDbObjectId reactId1, reactId2;
	AcDbObjectId windDictId;
	AcDbDictionary *pWindDict;
	AcDbDictionary *pNamedObj;
	Acad::ErrorStatus es;
	AcGeVector3d vec;
	CMAWinReact *pReact1, *pReact2;

	AcDbHandle handEnt1, handEnt2;
	char strHandle[17];

	AcGePoint3d ll2;
	double len;

	CMARectWindow *pRectWindow1;	// RectWindow class
	CMARectWindow *pRectWindow2;

	pCurDb = acdbHostApplicationServices()->workingDatabase();

	pRectWindow1 = new CMARectWindow;
	
	if(!getWindowInputParameters(pRectWindow1))
	{
		acutPrintf("\nError in window input parameters. ");
		if(pRectWindow1 != NULL)
		{
			delete pRectWindow1;
		}
		return;
	}

	es = appendToBlock(pRectWindow1, windId1, ACDB_MODEL_SPACE, pCurDb);
	if(es != Acad::eOk)
	{
		acutPrintf("\nError drawing custom entity ");
		if(pRectWindow1)
		{
			delete pRectWindow1;
			return;
		}
	}


	pRectWindow2 = new CMARectWindow();

	pRectWindow2->setWindowRows(pRectWindow1->getWindowRows());
	pRectWindow2->setWindowCols(pRectWindow1->getWindowCols());
	pRectWindow2->setWindowHeight(pRectWindow1->getWindowHeight());
	pRectWindow2->setWindowLength(pRectWindow1->getWindowLength());
	pRectWindow2->setWindowStartPoint(pRectWindow1->getWindowStartPoint());


	len = pRectWindow2->getWindowLength();
	vec.set(len, 0, 0);
	ll2 = pRectWindow2->getWindowStartPoint();
	ll2 = ll2 + vec;
	pRectWindow2->setWindowStartPoint(ll2);


	es = appendToBlock(pRectWindow2, windId2, ACDB_MODEL_SPACE, pCurDb);
	if(es != Acad::eOk)
	{
		acutPrintf("\nError drawing custom entity ");
		if(pRectWindow2)
		{
			delete pRectWindow2;
			return;
		}
	}
	

    pCurDb->getNamedObjectsDictionary(pNamedObj, AcDb::kForWrite);
	es = pNamedObj->getAt("WINDO_INFO", (AcDbObject*&)pWindDict, AcDb::kForWrite);
    if(es == Acad::eKeyNotFound)
    {
        pWindDict = new AcDbDictionary;
        pNamedObj->setAt("WINDO_INFO", pWindDict, windDictId);
    }
    pNamedObj->close();


	pReact1 = new CMAWinReact(windId2);
	pReact2 = new CMAWinReact(windId1);

	pReact1->setLwrLeftPoint(pRectWindow1->getWindowStartPoint());
	pReact2->setLwrLeftPoint(pRectWindow2->getWindowStartPoint());

	pRectWindow1->getAcDbHandle(handEnt1);
	handEnt1.getIntoAsciiBuffer(strHandle);
	es = pWindDict->setAt(strHandle, pReact1, reactId1);
	pReact1->close();

	pRectWindow2->getAcDbHandle(handEnt2);
	handEnt2.getIntoAsciiBuffer(strHandle);
	es = pWindDict->setAt(strHandle, pReact2, reactId2);
	pReact2->close();

	pRectWindow1->addPersistentReactor(reactId1);
	pRectWindow2->addPersistentReactor(reactId2);

	pRectWindow1->close();
	pRectWindow2->close();
	pWindDict->close();
}

// This is command 'PWI'
void pickWindowInfo()
{
	AcDbObject *pObj;
	CMARectWindow *pRectWindow = NULL;
	AcDbObjectId entId, objId;

	ads_name ename;
	ads_point pickpt;
	int rc;

	// Select the Window entity to get its handle
	rc = acedEntSel("\nSelect Window entity ", ename, pickpt);
	if(rc != RTNORM)
	{
		acutPrintf("\nError selecting Window entity ");
		return;
	}

	// Get the entity object ID
	acdbGetObjectId(entId, ename);

	// Open the enity for a read operation
	acdbOpenObject(pObj, entId, AcDb::kForRead);

	// Is pObj a RectWindow object ?
	pRectWindow = CMARectWindow::cast(pObj);
	if(!pRectWindow)
	{
		acutPrintf("\nObject is not a RectWindow ");
		pObj->close();
		return;
	}

	acutPrintf("\nWindow Type = %s", pRectWindow->m_strWindType);
	acutPrintf("\nRows = %d", pRectWindow->getWindowRows());
	acutPrintf("\nCols = %d", pRectWindow->getWindowCols());
	acutPrintf("\nLength = %.2lf", pRectWindow->getWindowLength());
	acutPrintf("\nHeight = %.2lf", pRectWindow->getWindowHeight());
	acutPrintf("\nStart Point = (%.2lf, %.2lf)",
				pRectWindow->getWindowStartPoint().x,
				pRectWindow->getWindowStartPoint().y);

	pRectWindow->close();
}

